﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MVCToolkit.Attributes;

namespace MVCToolkit
{
    public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        /// <summary>
        /// Gets the metadata for the specified property.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        /// <param name="containerType">The type of the container.</param>
        /// <param name="modelAccessor">The model accessor.</param>
        /// <param name="modelType">The type of the model.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <returns>The metadata for the property.</returns>
        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
                                                            Type containerType,
                                                            Func<object> modelAccessor,
                                                            Type modelType,
                                                            string propertyName)
        {
            var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
            attributes.OfType<ModelMetaDataAttribute>().ToList().ForEach(m => m.Process(modelMetadata));

            return modelMetadata;
        }
    }
}
